package com.yys.apm.config;

import com.yys.apm.common.enums.ErrorType;
import com.yys.apm.common.exception.BusinessException;
import com.yys.apm.common.response.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 *
 * @author Yingsheng Ye
 *
 */
@RestControllerAdvice
public class ExceptionHandlerConfig {

    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandlerConfig.class);

    /**
     * 业务异常.
     *
     * @param e exception
     * @return R
     */
    @ExceptionHandler(BusinessException.class)
    public Response exception(BusinessException e) {
        logger.error(">>>-------------------------------------");
        logger.error("业务异常信息,异常编码{}, ex:{}", e.getCode(), e.getMessage(), e);
        logger.error("-------------------------------------<<<");
        return new Response(e.getCode(), e.getMessage());
    }

    /**
     * 系统异常.
     *
     * @param e exception
     * @return R
     */
    @ExceptionHandler(Exception.class)
    public Response exception(Exception e) {
        logger.error(">>>-------------------------------------");
        logger.error("系统异常信息 ex:{}", e.getMessage(), e);
        logger.error("-------------------------------------<<<");
        return new Response(ErrorType.ERROR);
    }
}
